package com.recharge.service.impl;

import com.recharge.common.result.ResultDO;
import com.recharge.common.result.ResultSupport;
import com.recharge.common.util.Constant;
import com.recharge.core.mapper.OrderMapper;
import com.recharge.core.mapper.ProviderOrderMapper;
import com.recharge.domain.vo.MonitorDO;
import com.recharge.domain.vo.MontiorProvider;
import com.recharge.service.MonitorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class MonitorServiceImpl implements MonitorService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private ProviderOrderMapper providerOrderMapper;

    public ResultDO monitor() {

        ResultDO result = new ResultSupport();

        MonitorDO domain = new MonitorDO();

        long processTotal = 0L;
        long successTotal = 0L;
        long failTotal = 0L;
        long unconfirmedTotal = 0L;

        List<Map<String, Object>> providerList = null;

        try {
            processTotal = orderMapper.totalOrderByStatus(Constant.RECHARGE_ORDER_STATUS_5);
            successTotal = orderMapper.totalOrderByStatus(Constant.RECHARGE_ORDER_STATUS_3);
            failTotal = orderMapper.totalOrderByStatus(Constant.RECHARGE_ORDER_STATUS_4);
            unconfirmedTotal = orderMapper.totalOrderByStatus(Constant.RECHARGE_ORDER_STATUS_7);
        } catch (Exception e) {
            e.printStackTrace();
        }

        domain.setProcessTotal(processTotal);
        domain.setSuccessTotal(successTotal);
        domain.setFailTotal(failTotal);
        domain.setUnconfirmedTotal(unconfirmedTotal);

        try {
            providerList = providerOrderMapper.totalProviderOrderByProvider(Constant.PROVIDER_ORDER_STATUS_fail);
        } catch (Exception e) {
            e.printStackTrace();
        }

        List<MontiorProvider> list = null;
        list = new ArrayList<>();
        for(Map<String, Object> map : providerList) {
            MontiorProvider montiorProvider = new MontiorProvider();
            montiorProvider.setOrderTotal((Long) map.get("total"));
            montiorProvider.setProviderName((String) map.get("provider_name"));
            montiorProvider.setProcessTime((Long) map.get("process_time"));
            list.add(montiorProvider);
        }
        domain.setFailProviderList(list);
        try {
            providerList = providerOrderMapper.totalProviderOrderByProvider(Constant.PROVIDER_ORDER_STATUS_2);
        } catch (Exception e) {
            e.printStackTrace();
        }
        list = new ArrayList<>();
        for(Map<String, Object> map : providerList) {
            MontiorProvider montiorProvider = new MontiorProvider();
            montiorProvider.setOrderTotal((Long) map.get("total"));
            montiorProvider.setProviderName((String) map.get("provider_name"));
            montiorProvider.setProcessTime((Long) map.get("process_time"));
            list.add(montiorProvider);
        }
        domain.setSuccessProviderList(list);
        try {
            providerList = providerOrderMapper.totalProviderOrderByProvider(Constant.PROVIDER_ORDER_STATUS_3);
        } catch (Exception e) {
            e.printStackTrace();
        }
        list = new ArrayList<>();
        for(Map<String, Object> map : providerList) {
            MontiorProvider montiorProvider = new MontiorProvider();
            montiorProvider.setOrderTotal((Long) map.get("total"));
            montiorProvider.setProviderName((String) map.get("provider_name"));
            montiorProvider.setProcessTime((Long) map.get("process_time"));
            list.add(montiorProvider);
        }
        domain.setRechargeProviderList(list);

        result.setModel(ResultDO.FIRST_MODEL_KEY, domain);
        return result;
    }
}
